package com.sjzsqjy.asdtiang.mysql;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.hibernate.annotations.GenericGenerator;

import com.sjzsqjy.asdtiang.action.AdminAction;
import com.sjzsqjy.asdtiang.action.ObjectAction;
import com.sjzsqjy.asdtiang.util.MD5;

/**
 * HosUser entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "hos_user", catalog = "hospital", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
public class HosUser implements java.io.Serializable {

	// Fields

	private Long id;
	private Long version;
	private Boolean enabled;
	private String passwd;
	private String userRealName;
	private String username;
	private Boolean emailShow;
	private String email;
	private String description;
	private Set<HosRole> hosRoles = new HashSet<HosRole>(0);

	// Constructors

	/** default constructor */
	public HosUser() {
	}

	/** minimal constructor */
	public HosUser(Long version, Boolean enabled, String passwd,
			String userRealName, String username, Boolean emailShow,
			String email, String description) {
		this.version = version;
		this.enabled = enabled;
		this.passwd = passwd;
		this.userRealName = userRealName;
		this.username = username;
		this.emailShow = emailShow;
		this.email = email;
		this.description = description;
	}

	/** full constructor */
	public HosUser(Long version, Boolean enabled, String passwd,
			String userRealName, String username, Boolean emailShow,
			String email, String description, Set<HosRole> hosRoles) {
		this.version = version;
		this.enabled = enabled;
		this.passwd = passwd;
		this.userRealName = userRealName;
		this.username = username;
		this.emailShow = emailShow;
		this.email = email;
		this.description = description;
		this.hosRoles = hosRoles;
	}

	// Property accessors
	@Id
	@GenericGenerator(name = "persistenceGenerator", strategy = "increment")
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "version")
	public Long getVersion() {
		return this.version;
	}

	public void setVersion(Long version) {
		this.version = version;
	}

	@Column(name = "enabled")
	public Boolean getEnabled() {
		return this.enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	@Column(name = "passwd", nullable = false)
	public String getPasswd() {
		return this.passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	@Column(name = "user_real_name", nullable = false)
	public String getUserRealName() {
		return this.userRealName;
	}

	public void setUserRealName(String userRealName) {
		this.userRealName = userRealName;
	}

	@Column(name = "username", unique = true, nullable = false)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "email_show")
	public Boolean getEmailShow() {
		return this.emailShow;
	}

	public void setEmailShow(Boolean emailShow) {
		this.emailShow = emailShow;
	}

	@Column(name = "email")
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "description")
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "hos_role_people", catalog = "hospital", joinColumns = { @JoinColumn(name = "hos_user_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "hos_role_id", nullable = false, updatable = false) })
	public Set<HosRole> getHosRoles() {
		return this.hosRoles;
	}

	public void setHosRoles(Set<HosRole> hosRoles) {
		this.hosRoles = hosRoles;
	}
	public static HosUser validate(HosUser object,AdminAction adminAction){
		if(null==object.getUsername()||object.getUsername().equals("")){
			return null;
		}
		if(null==object.getPasswd()||object.getPasswd().equals("")){
			return null;
		}
		object.setVersion(1l);
		object.setPasswd(MD5.getMD5().getMD5(object.getPasswd().getBytes()));
		return object;
	}

}